#ifndef QSQL_DUCKDB_P_H
#define QSQL_DUCKDB_P_H

#include "qsql_duckdb.h"
#include <QtSql/private/qsqldriver_p.h>
#include <QtSql/private/qsqlresult_p.h>
#include <QtSql/qsqlrecord.h>
#include <QtCore/qvariant.h>
#include <QtCore/qdatetime.h>
#include <QtCore/qstringlist.h>
#include <QtCore/qvector.h>
#include <QtCore/qmutex.h>
#include <QtCore/qhash.h>

// DuckDB原生API头文件
#include "duckdb.h"

QT_BEGIN_NAMESPACE

// DuckDB数据库实例管理器 - 支持多连接共享同一个数据库实例
class QDuckdbDatabaseManager
{
public:
    struct DatabaseInstance {
        duckdb_database db;
        int refCount;
        bool isReadOnly;
        QString dbPath;
        
        DatabaseInstance() : refCount(0), isReadOnly(false) {}
    };
    
    static QDuckdbDatabaseManager* instance();
    
    // 获取或创建数据库实例
    duckdb_database getDatabaseInstance(const QString& dbPath, bool readOnly, duckdb_config config, QString& errorMsg);
    
    // 释放数据库实例引用
    void releaseDatabaseInstance(const QString& dbPath, bool readOnly);
    
private:
    QDuckdbDatabaseManager() = default;
    ~QDuckdbDatabaseManager();
    
    QMutex m_mutex;
    QHash<QString, DatabaseInstance*> m_databases; // key: dbPath + "_" + (readOnly ? "RO" : "RW")
    
    QString makeKey(const QString& dbPath, bool readOnly);
};

// 定义增强的调试输出宏，包含文件名、行号和函数名
#include <cstdio>
#include <cstdarg>

// 定义一个可变参数的调试输出函数
inline void duckdb_debug_printf(const char* file, int line, const char* func, const char* format, ...) {
    printf("[%s:%d] %s: ", file, line, func);
    va_list args;
    va_start(args, format);
    vprintf(format, args);
    va_end(args);
    printf("\n");
}

#define DUCKDB_DEBUG(...) duckdb_debug_printf(__FILE__, __LINE__, Q_FUNC_INFO, __VA_ARGS__)

class QDuckdbDriverPrivate : public QSqlDriverPrivate
{
    Q_DECLARE_PUBLIC(QDuckdbDriver)
    friend class QDuckdbResultPrivate;
    duckdb_database db;
    duckdb_connection conn;
    QString errorMessage;
    QString lastQuery;
    bool inTransaction;    // 跟踪事务状态
    bool isReadOnly;       // 是否为只读连接
    QString dbPath;        // 数据库路径，用于管理共享实例

public:
    QDuckdbDriverPrivate();
    ~QDuckdbDriverPrivate();
    void handleError(const char *error);
    void splitSchemaTableName(const QString &name, QString &schema, QString &table);
};

class QDuckdbResultPrivate : public QSqlResultPrivate
{
    Q_DECLARE_PUBLIC(QDuckdbResult)
    Q_DECLARE_SQLDRIVER_PRIVATE(QDuckdbDriver)

    // DuckDB相关成员
    duckdb_prepared_statement stmt;
    duckdb_result result;
    
    int currentRow;
    int rowCount;
    int affectedRows;
    QSqlRecord record;
    QVector<QVariant> boundValues;
    QString errorMessage;

public:
    QDuckdbResultPrivate(QDuckdbResult *q, const QDuckdbDriver *db);
    ~QDuckdbResultPrivate();
    void cleanup();

    // DuckDB相关方法
    void buildRecordFromResult();
    bool prepare(const QString &query);
    bool execute();
    bool bindValue(int pos, const QVariant &val);
    QVariant dataValue(int col);
    bool isValueNull(int col);
    QString lastError() const;
};

QT_END_NAMESPACE

#endif // QSQL_DUCKDB_P_H